package com.iad.controller.command
{
  import com.adobe.cairngorm.commands.Command;
  import com.adobe.cairngorm.control.CairngormEvent;
  import com.adobe.cairngorm.control.CairngormEventDispatcher;
  import com.iad.controller.event.*;
  import com.iad.controller.service.*;
  import com.iad.model.*;
  import com.iad.model.valueobject.*;
  import com.iad.view.component.*;
  import com.iad.view.form.*;
  import com.iad.view.util.ascb.util.*;
  
  import mx.containers.ViewStack;
  import mx.core.UIComponent;
  
  public class AdminCommand implements Command
  {
    [Bindable]
    private var model:ModelLocatorImpl = ModelLocatorImpl.getInstance();
    
 
    public function execute(event:CairngormEvent):void {
      switch ( event.type ) {
        case AdminEvent.EDIT_MODE:
          model.EDIT_MODE = true;
          editMode();
        break;
        case AdminEvent.CANCEL_MODE:
          // ALSO NEED TO ROLL BACK TO MODEL AT TIME OF LAST EDIT BUTTON CLICK
          model.EDIT_MODE = false;
          cancelMode();
        break;
        case AdminEvent.PREVIEW_MODE:
          model.EDIT_MODE = false;
          previewMode();
        break;
        case AdminEvent.PUBLISH_MODE:
          publishMode(model.EDIT_MODE);
          model.EDIT_MODE = false;
        break;
     }
    }
    
    private function previewMode():void {
      var currentStack:ViewStack = model.app.mainView.mainViews;
      
      if (model.app.currentState == model.ARTIST_VIEW) {
        currentStack = model.app.artistView.artistViews;
      }
      
      for each(var contentPage:UIComponent in currentStack.getChildren()) {
        if (contentPage is BasicContentPage) {
          (contentPage as BasicContentPage).doPreview(); 
        }
      }
    }
    
    
    private function publishMode(fromEdit:Boolean):void {
      var currentStack:ViewStack = model.app.mainView.mainViews;
      // only grab items if we are either in PREVIEW or comiming directly from EDIT, otherwise just save.
      if (fromEdit) {
        if (model.app.currentState == model.ARTIST_VIEW) {
          currentStack = model.app.artistView.artistViews;
        }
      
        for each(var contentPage:UIComponent in currentStack.getChildren()) {
          if (contentPage is BasicContentPage) {
            (contentPage as BasicContentPage).doPreview(); 
          }
        }
      }
      CairngormEventDispatcher.getInstance().dispatchEvent(new PageEvent(PageEvent.SAVE_PAGES, null));
    }
    
    
    private function editMode():void {
      var currentStack:ViewStack = model.app.mainView.mainViews;
      
      if (model.app.currentState == model.ARTIST_VIEW) {
        currentStack = model.app.artistView.artistViews;
      }
      for each(var contentPage:UIComponent in currentStack.getChildren()) {
        if (contentPage is BasicContentPage) {
          (contentPage as BasicContentPage).renderEditPage(); 
        }
      }
    }
    
    
    private function cancelMode():void {
      CairngormEventDispatcher.getInstance().dispatchEvent(new PageEvent(PageEvent.GET_PAGES, 0));
    }
     
  } 
}
    